<?php
/**
 * Created by PhpStorm.
 * User: Akuma
 * Date: 2016/11/28
 * Time: 16:27
 */
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
    function test()
    {
        $redb = new \Think\Model('Reworld');
        $total = $redb->count();
        $page = new \Tools\Page($total);
        $sql = 'select * from reworld order by id desc '.$page->limit;
        $arr_all = $redb->query($sql);
        $pagelist = $page->fpage(array(3, 4, 5, 6, 7, 8));
        $this->assign('pagelist', $pagelist);
        array_walk($arr_all,'test1');
        $this->assign('arr_all', $arr_all);
        $this->display();
    }

    function outcome()
    {
        /**
         * 只处理至少六条的结果
         */
        if (($c = count($_POST))<6) {

            $this->redirect('test',array('num' => $c), 2,'做了'.$c.'太少');
            exit();
        }
        /**
         * 使用I()来代替$_POST必须带点号:I('post.')
         */
        $redb = M('Reworld');
        $arr_outcome = I('POST.');
        ksort($arr_outcome);
        var_dump($arr_outcome);
        $yes_id = '';
        $all_id = '';
        $i = 0;
        foreach ($arr_outcome as $k => $v) {
            if (0 == $i) {
                $all_id = $k;
            } else {
                $all_id .= ",$k";
            }
            if ("YES" == $v) {
                if (0 == $i) {
                    $yes_id = $k;
                } else {
                    $yes_id .= ",$k";
                }
            }
            $i = 1;
        }
        if('' != $yes_id) {
            $sql = "update reworld set rit = rit + 1 where id in ($yes_id)";
            var_dump($redb->execute($sql));
        }
        $sql = "update reworld set click = click + 1 where id in ($all_id)";
        var_dump($redb->execute($sql));
    }

    function plusKnow()
    {
        $redb = D('Reworld');
        if (!empty($_POST)) {
            $info = $redb->create();
            if ($info == false) {
                echo json_encode($redb->getError());
            }else{
                /*
                 * 会自动转html实体和单引号
                 */
                $z = $redb->add();
                if ( $z == false) {
                    $this->redirect('plusKnow', array(), 2, '添加失败');
                } else {
                    session('topic', I("post.topic"));
                    $this->redirect('plusKnow', array(), 2, '添加成功');
                }
            }
        } else {
            if (!empty(session('topic'))) {
                $this->assign('last_topic', session('topic'));
            }
            $arr_topic = $redb->field('topic')->group('topic')->select();
            $this->assign('topic', $arr_topic);
            $this->display();
        }

    }

    function delete($id)
    {
        $redb = M('Reworld');
        $z = $redb -> delete($id);
        if (false != $z) {
            /*
             * 一定要终止脚本运行,否则开启调试模式时,会执行调试模式的代码
             */
//            echo $id;
//            exit;
            /*
             * 或者使用Ajax返回$this->ajaxReturn($data);
             */
            $this->ajaxReturn($id);
        }
    }

    function upd($id)
    {
        $redb = D('Reworld');
        if (!empty($_POST)) {
            $info = $redb ->create();
            if ($info == false) {
                var_dump($info->getError);
            } else {
                $z = $redb->save();
                if ($z == false) {
                    echo '失败';
                } else {
                    $this->redirect('Test/test');
                }
            }
        } else {
            $arr_upd = $redb -> find($id);
            $this -> assign('arr_upd', $arr_upd);
            $this -> display();
        }

    }
}